package com.yyw.foodie.controller.base;


import com.yyw.foodie.bo.users.UsersBo;
import com.yyw.foodie.common.Result;
import com.yyw.foodie.pojo.UsersEntity;
import com.yyw.foodie.service.UsersService;
import com.yyw.foodie.utils.BindingResultJoinError;
import com.yyw.foodie.utils.CookieUtils;
import com.yyw.foodie.utils.JsonUtils;
import com.yyw.foodie.vo.UsersVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 用户表（用户相关）  前端控制器
 * </p>
 *
 * @author yiyw
 * @since 2021-05-22
 */
@Api(value = "用户中心相关", tags = "用户中心相关接口")
@RestController
@RequestMapping("/userInfo")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @ApiOperation(value = "获取用户信息", notes = "用户中心展示用户信息")
    @GetMapping
    public Result getUserInfo(@ApiParam(value = "用户ID") @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(userId)) {
            return Result.errorMsg("非法参数");
        }
        UsersEntity usersEntity = usersService.getById(userId);
        if (usersEntity == null) {
            return Result.ok(new UsersVo());
        }
        UsersVo usersVo = new UsersVo();
        BeanUtils.copyProperties(usersEntity, usersVo);
        usersVo.setNickname(usersEntity.getNickName());
        usersVo.setUsername(usersEntity.getUserName());
        usersVo.setRealname(usersEntity.getRealName());
        return Result.ok(usersVo);
    }


    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    @PostMapping("/update")
    public Result updateUserInfo(@Valid @RequestBody UsersBo usersBo, BindingResult bindingResult,
                                 HttpServletRequest request, HttpServletResponse response) {
        // 0. 校验数据
        Map<String, String> map = BindingResultJoinError.joinErrorMsg(bindingResult);
        if (map != null) {
            return Result.errorMap(map);
        }
        // 2. 查找数据
        UsersEntity entity = usersService.getById(usersBo.getId());
        if (entity != null) {
            BeanUtils.copyProperties(usersBo, entity);
            entity.setUserName(usersBo.getUsername());
            entity.setNickName(usersBo.getNickname());
            entity.setRealName(usersBo.getRealname());
        }
        // 3. 修改数据
        usersService.updateById(entity);
        // 4. 重置cookie信息
        CookieUtils.setCookie(request, response, "user", JsonUtils.objectToJson(usersBo), true);

        return Result.ok();
    }


    @PostMapping("/uploadFace")
    @ApiOperation(value = "修改用户头像", notes = "上传用户头像，修改头像信息")
    public Result uploadFace(@ApiParam(value = "用户ID") @RequestParam String userId,
                             @ApiParam(value = "用户头像") MultipartFile file,
                             HttpServletRequest request, HttpServletResponse response) {
        usersService.updateFace(userId, file);
        // 4. 重置cookie信息
        UsersEntity entity = usersService.getById(userId);
        UsersVo usersVo = new UsersVo();
        BeanUtils.copyProperties(entity, usersVo);
        usersVo.setUsername(entity.getUserName());
        usersVo.setNickname(entity.getNickName());
        usersVo.setRealname(entity.getRealName());
        CookieUtils.setCookie(request, response, "user", JsonUtils.objectToJson(usersVo), true);
        return Result.ok();
    }

}

